package cn.swenty.backstage.framework.common.util;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;

import cn.swenty.backstage.framework.common.tree.NodeSearchType;
import cn.swenty.backstage.framework.common.tree.TreeNode;
import cn.swenty.backstage.framework.util.IdGenerator;

/**
 * 对树操作的工具类
 * @author 王永明
 * @since Mar 22, 2010 11:15:49 AM
 */
public class TreeUtil {
	
	/**将所有节点连起来组成一棵树,返回根节点*/
	public static List createTree(List nodes){
		clear(nodes);
		CollectionUtil.sort(nodes, "displayNo");
		List<TreeNode> root=new ArrayList();
		Map<String,TreeNode> map=BeanUtil.toMap(nodes,"id");
		for(Object o:nodes){
			TreeNode node=(TreeNode)o;
			if(node.getChildren()==null){
				node.setChildren(new ArrayList());
			}
			TreeNode parent=map.get(node.getParentId());
			if(parent==null||parent.getId().equals(node.getId())){
				root.add(node);
				node.setDisplayNo(root.size());
				node.setParentId(null);
				continue;
			}
			List children=parent.getChildren();
			if(children==null){
				children=new ArrayList();
			}
			children.add(node);
			node.setParent(parent);
			node.setDisplayNo(children.size());
			parent.setChildren(children);	
		}
		return root;
	}
	
	/**将所节点按树形排序*/
	public static List sort(List nodes){
		
		List result=new ArrayList(nodes);
		nodes.clear();
		List<TreeNode> root=TreeUtil.createTree(result);
		for(TreeNode node:root){
			add(nodes,node);
		}
		return nodes;
	}
	
	public static void add(List list,TreeNode root){
		list.add(root);
		for(Object node:root.getChildren()){
			add(list,(TreeNode) node);
		}
	}
	
	/**清除所有上下级关系*/
	public static void clear(List nodes){
		List<TreeNode> nde=nodes;
		for(TreeNode node:nde){
			node.setParent(null);
			node.setChildren(null);
		}
	}

	/**获得某个节点下级节点,递归获取*/
	private static void addSubs(List list, TreeNode root){
		list.add(root);
		List<TreeNode> children=root.getChildren();
		if(list!=null&&list.size()!=0){
			for(TreeNode n:children){
				addSubs(list,n);
			}
		}
	}
	
	/**获得某个节点二级节点,递归获取*/
	private static TreeNode getSecondNode(TreeNode root){
		TreeNode parent=root.getParent();
		if(parent!=null){
			root=getSecondNode(parent,root);
		}
		return root;
	}
	
	/**获得某个节点二级节点,递归获取*/
	private static TreeNode getSecondNode(TreeNode parent,TreeNode root){
		TreeNode parentP=parent.getParent();
		if(parentP!=null){
			root=getSecondNode(parentP,parent);
		}
		return root;
	}
	
	
	/**重建主键*/
	public static void reBuildPk(List list, String rex) {
		List<TreeNode> nde=list;
		createTree(nde);
		for(TreeNode n:nde){
			n.setId(IdGenerator.getNextId());
		}
		for(TreeNode n:nde){
			if(n.getParent()!=null){
				n.setParentId(n.getParent().getId());
			}else{
				n.setParentId(null);
			}
		}
		
	}
	
	/**获得与某个节点相关节点*/
	public static List getNodes(List<TreeNode> list ,NodeSearchType nodeSearchType,String nodeId){
		//createTree(list);
		List result=new ArrayList();
		switch(nodeSearchType){
		case all:
			return list;
		case current:
			
			for(TreeNode node:list){
				if(node.getId().equals(nodeId)){
					result.add(node);
					return result;
				}
			}
		case cascade:
			for(TreeNode node:list){
				if(node.getId().equals(nodeId)){
					addSubs(result,node);
					return result;
				}
			}
		case sub:
			for(TreeNode node:list){
				if(node.getId().equals(nodeId)){
					List<TreeNode> sub=node.getChildren();
					for(TreeNode o:sub){
						addSubs(result,o);
					}	
					return result;
				}
			}
		case secondAll:
			for(TreeNode node:list){
				if(node.getId().equals(nodeId)){
					TreeNode nodeSecond=getSecondNode(node);
					addSubs(result,nodeSecond);
					return result;
				}
			}
			default:
					throw new RuntimeException("未知类型:"+nodeSearchType);
		}
	
	}
	
	public static void setLevel(List list){
		List<TreeNode> root=TreeUtil.createTree(list);
		setLevel(root,0);
	}
	
	private static void setLevel(List<TreeNode> list,int levels ){
		if(list==null||list.size()==0){
			return;
		}
		for(TreeNode node:list){
			BeanUtil.setValue(node, "levels", levels+"");
			setLevel(node.getChildren(),levels+1);
		}
	}
}





